package com.shanghaionstar.notifysetting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.notifysetting.presenter.NotifyPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * 车辆故障诊断提醒渠道设置
 * Created by Yonglang Hong on 2017/3/17.
 */
@ContentView(R.layout.activity_setnotify_da)
public class SetDANotifyActivity extends SwipeActivity implements IMVPView {
    private NotifyPresenter mPresenter;
    private static final int CHECK_SMS = 100;
    private static final int CHECK_APP = 101;
    private static final int CHECK_MAIL = 102;

    @FindViewById
    private CustomLineLayout cll_setNotifyApp, cll_setNotifySms, cll_setNotifyMail;

    @FindViewById
    private TextView tv_tipNotifyApp, tv_tipNotifySms, tv_tipNotifyMail, tv_renewalTip;

    @FindViewById
    private Button btn_renewal;
    @FindViewById
    private TopLayout top_layout;

    @FindViewById
    private ProgressLayout progress_layout;

    private NotifyConfigRes mNotifyConfig;

    private int whichCheck;
    private boolean isPreSelected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NotifyPresenter(getScheduler());
        mPresenter.setView(this);
        initView();
    }

    private void initView() {
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.DASetting_ClickBack.getStringValue());
                finish();
            }
        });
        Intent it = getIntent();
        if (it != null)
            mNotifyConfig = (NotifyConfigRes) it.getSerializableExtra(NotifyPresenter.IBType.TYPE_DA);
        if (mNotifyConfig != null) {
            String smsChecked = mNotifyConfig.getSms_checked();
            boolean notifySms = !TextUtils.isEmpty(smsChecked) && smsChecked.equalsIgnoreCase("1");
            cll_setNotifySms.setSelected(notifySms);
            SmsEmptyHelper helper = new SmsEmptyHelper(this,mNotifyConfig);
            helper.handleSms(tv_tipNotifySms,R.string.notify_da_sms_tip);
//            tv_tipNotifySms.setText(String.format(getString(R.string.notify_da_sms_tip), CommonUtil.replaceString(mNotifyConfig.getPhone(), false)));
            tv_tipNotifySms.setVisibility(notifySms ? View.VISIBLE : View.GONE);
//            tv_tipNotifySms, tv_tipNotifyApp, tv_tipNotifyPhone
            String mobileChecked = mNotifyConfig.getMobile_checked();
            boolean notifyApp = !TextUtils.isEmpty(mobileChecked) && mobileChecked.equalsIgnoreCase("1");
            cll_setNotifyApp.setSelected(notifyApp);
            tv_tipNotifyApp.setText(getString(R.string.notify_da_app_tip));
            tv_tipNotifyApp.setVisibility(notifyApp ? View.VISIBLE : View.GONE);
            cll_setNotifyApp.setSwitchVisable(false);
            String mailChecked = mNotifyConfig.getMail_checked();
            boolean notifyMail = !TextUtils.isEmpty(mailChecked) && mailChecked.equalsIgnoreCase("1");
            cll_setNotifyMail.setSelected(notifyMail);
            helper.handleEmail(tv_tipNotifyMail,R.string.notify_da_mail_tip);
//            tv_tipNotifyMail.setText(String.format(getString(R.string.notify_da_mail_tip), CommonUtil.replaceString(mNotifyConfig.getMail(), true)));
            tv_tipNotifyMail.setVisibility(notifyMail ? View.VISIBLE : View.GONE);

//            cll_setNotifySms.setSelected(mNotifyConfig.getSms_checked() == 1 ? true : false);
//            cll_setNotifyApp.setSelected(mNotifyConfig.getMobile_checked() == 1 ? true : false);
//            cll_setNotifyMail.setSelected(mNotifyConfig.getMail_checked() == 1 ? true : false);
        }
        cll_setNotifySms.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.DASetting_SMSOFF.getStringValue():FunctionNameFindId.DASetting_SMSON.getStringValue());
                onRealCheckedChanged(buttonView, CHECK_SMS);
            }
        });
        cll_setNotifyApp.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.DASetting_APPNotificationOFF.getStringValue():FunctionNameFindId.DASetting_APPNotificationON.getStringValue());
                onRealCheckedChanged(buttonView, CHECK_APP);
            }
        });
        cll_setNotifyMail.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.DASetting_EmailNotificationOFF.getStringValue():FunctionNameFindId.DASetting_EmailNotificationON.getStringValue());
                onRealCheckedChanged(buttonView, CHECK_MAIL);
            }
        });
    }

    private void onRealCheckedChanged(CompoundButton buttonView, int wh) {
        whichCheck = wh;
        isPreSelected = buttonView.isSelected();
        buttonView.setSelected(!isPreSelected);
        switch (wh) {
            case CHECK_SMS:
                mNotifyConfig.setSms_checked(!isPreSelected ? "1" : "0");
                break;
            case CHECK_MAIL:
                mNotifyConfig.setMail_checked(!isPreSelected ? "1" : "0");
                break;
            case CHECK_APP:
                mNotifyConfig.setMobile_checked(!isPreSelected ? "1" : "0");
                break;
        }
        mPresenter.setNotifyConfig(mNotifyConfig);
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        onSetConfigFailed();
    }

    private void onSetConfigFailed() {
        switch (whichCheck) {
            case CHECK_SMS:
                cll_setNotifySms.setSelected(isPreSelected);
                mNotifyConfig.setSms_checked(isPreSelected ? "1" : "0");
                tv_tipNotifySms.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
            case CHECK_MAIL:
                cll_setNotifyMail.setSelected(isPreSelected);
                mNotifyConfig.setMail_checked(isPreSelected ? "1" : "0");
                tv_tipNotifyMail.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
            case CHECK_APP:
                cll_setNotifyApp.setSelected(isPreSelected);
                mNotifyConfig.setMobile_checked(isPreSelected ? "1" : "0");
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public void onReponseSuccess(Object object) {
        if (object != null && object instanceof BaseResponse) {
            BaseResponse response = (BaseResponse) object;
            if (!response.code.equalsIgnoreCase("E0000")) {
                onSetConfigFailed();
            } else {
                onSuccessLayout();
            }
        }
    }

    private void onSuccessLayout() {
        switch (whichCheck) {
            case CHECK_APP:
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
            case CHECK_SMS:
                tv_tipNotifySms.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
            case CHECK_MAIL:
                tv_tipNotifyMail.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }
}

